﻿using System;
using System.Collections.Generic;
using ProtoBuf;

namespace LingDong.IndexEntities.Dictionary
{
    [ProtoContract]
    [Serializable]
    public class DictionaryIndexEntity
    {
        #region Constructor

        public DictionaryIndexEntity()
        {
            Init();
        }

        public DictionaryIndexEntity(int id)
        {
            Init();
            this.ID = id;
        }

        public DictionaryIndexEntity(int id, TermHTMLPosition pos, PanGu.WordType type)
        {
            Init();

            this.ID = id;
            this.TermType = type;
            if (pos == TermHTMLPosition.Title)
            {
                TotalTFInTitle = 1;
            }
        }

        private void Init()
        {
            ID = DF = TotalTF = TotalTFInTitle = 0;
            IsStopped = false;
            TermType = PanGu.WordType.None;
            InvertedList = new Dictionary<int, InvertedListEntity>();
            InvertedListOffset = 0;
        }

        #endregion

        
        #region ToString

        public override string ToString()
        {
            return String.Format("TID: {0}, DF: {1}, TotalTf: {2}, TFTitle: {3}, IsStopped: {4}, POS: {5}, Type: {6}",
                ID, DF, TotalTF, TotalTFInTitle, IsStopped, TermPos, TermType);
        }

        #endregion
        

        #region Feilds

        /// <summary>
        /// Term ID
        /// </summary>
        [ProtoMember(1)]
        public int ID { get; set; }

        /// <summary>
        /// Document frequency
        /// </summary>
        [ProtoMember(2)]
        public int DF { get; set; }

        /// <summary>
        /// Total term frequency in all documents
        /// </summary>
        [ProtoMember(3)]
        public int TotalTF { get; set; }

        /// <summary>
        /// Total term frequency in title in all documents
        /// </summary>
        [ProtoMember(4)]
        public int TotalTFInTitle { get; set; }

        /// <summary>
        /// Is the term a stop word
        /// Stop words are declared in LingDongStopWord.txt
        /// </summary>
        [ProtoMember(5)]
        public bool IsStopped { get; set; }

        /// <summary>
        /// POS of the term, appears most in the index
        /// </summary>
        public PanGu.POS TermPos { get; set; }
        [ProtoMember(6)]
        private int TermPosInt
        {
            get { return (int)TermPos; }
            set { TermPos = (PanGu.POS)value; }
        }

        /// <summary>
        /// Type of the term
        /// English, SimplifiedChinese, TraditionalChinese, Numeric, Symbol, Space, Synonym
        /// </summary>
        [ProtoMember(7)]
        public PanGu.WordType TermType { get; set; }

        /// <summary>
        /// Stream offset of inverted list file
        /// </summary>
        [ProtoMember(8)]
        public long InvertedListOffset { get; set; }

        private Dictionary<int, InvertedListEntity> invertedList;

        /// <summary>
        /// (DocID, InvertedListEntity)
        /// </summary>
        public Dictionary<int, InvertedListEntity> InvertedList
        {
            get { return invertedList; }
            set { invertedList = value; }
        }

        #endregion
    }
}
